package com.example.g32.Req

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.base.room.UserEntity
import com.example.g32.R
import com.example.g32.databinding.ActivityReqBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

@AndroidEntryPoint
@Route(path = "/app/ReqActivity")
class ReqActivity : BaseActivity<ActivityReqBinding, UserIntent, UserViewModel>() {
    override val binding by lazy {
        ActivityReqBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<UserViewModel>()

    var code = ""
    var time = 10
    var timer: Timer? = null

    var uname = ""
    var mycode = ""
    var pass = ""
    var pass2 = ""
    override fun initView() {
        binding.registerSendcode.apply {
            setOnClickListener {
                isEnabled = false
                code = Math.round(Math.random() * 10000).toString()
                ToastUtils.showLong(code)
                lifecycleScope.launch(Dispatchers.IO) {
                    timer = Timer()
                    timer!!.schedule(timerTask {
                        lifecycleScope.launch(Dispatchers.Main) {
                            if (time != 0) {
                                setText("倒计时（${time--}s）")
                            } else {
                                setText("重新获取")
                                time = 10
                                isEnabled = true
                                timer?.cancel()
                            }
                        }
                    }, 0, 1000)
                }
            }
        }

        binding.registerPass2.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                uname = binding.registerUname.text.toString()
                mycode = binding.registerCode.text.toString()
                pass = binding.registerPass.text.toString()
                pass2 = binding.registerPass2.text.toString()
                if (uname.isNullOrEmpty() || mycode.isNullOrEmpty() || pass.isNullOrEmpty() || pass2.isNullOrEmpty()) {
                    ToastUtils.showLong("输入内容不能为空")
                } else {
                    binding.btnRegister.isEnabled = true
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })


        binding.btnRegister.setOnClickListener {
            uname = binding.registerUname.text.toString()
            mycode = binding.registerCode.text.toString()
            pass = binding.registerPass.text.toString()
            pass2 = binding.registerPass2.text.toString()
            if (!uname.matches(Regex("[A-z0-9_]{2,8}"))) {
                ToastUtils.showLong("不是合法用户名")
                return@setOnClickListener
            }
            if (code != this.code) {
                ToastUtils.showLong("验证码校验失败")
                return@setOnClickListener
            }
            if (pass != pass2) {
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }

            vm.sendIntent(UserIntent.register(uname, pass))
           // vm.sendIntent(UserIntent.registerRoom(UserEntity(null, uname, pass, 20)))
        }

        binding.goLogin.setOnClickListener {
            ARouter.getInstance().build("/app/LoginActivity").navigation()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        ToastUtils.showLong("注册成功")
                    }

                    is UiState.onError -> {
                        Log.i("lr", it.msg)
                    }

                    else -> {}
                }
            }
        }
    }
}